using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;

namespace Schedulr.Helpers
{
    /// <summary>
    /// Contains helper methods for conversions.
    /// </summary>
    internal static class Converter
    {
        /// <summary>
        /// Gets the number of bytes as a string.
        /// </summary>
        /// <param name="bytes">The number of bytes.</param>
        /// <returns>The size in megabytes of the given number of bytes.</returns>
        public static string GetBytesAsString(long bytes)
        {
            double kiloBytes = (bytes / ((double)1024));
            if (kiloBytes > 1024)
            {
                return string.Format(CultureInfo.CurrentCulture, "{0} MB", (kiloBytes / ((double)1024)).ToString("f2", CultureInfo.CurrentCulture));
            }
            else
            {
                return string.Format(CultureInfo.CurrentCulture, "{0} KB", kiloBytes.ToString("f2", CultureInfo.CurrentCulture));
            }
        }

        /// <summary>
        /// Converts a string collection to an array.
        /// </summary>
        /// <param name="collection">The collection to convert.</param>
        /// <returns>The strings in the collection.</returns>
        public static string[] CollectionToArray(StringCollection collection)
        {
            List<string> strings = new List<string>();
            if (collection != null)
            {
                foreach (string str in collection)
                {
                    strings.Add(str);
                }
            }
            return strings.ToArray();
        }

        /// <summary>
        /// Converts a string array to a collection.
        /// </summary>
        /// <param name="array">The array to convert.</param>
        /// <returns>The string collection.</returns>
        public static StringCollection ArrayToCollection(string[] array)
        {
            StringCollection strings = new StringCollection();
            foreach (string str in array)
            {
                strings.Add(str);
            }
            return strings;
        }
    }
}